﻿var statustime = true;
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var main = false;

$(document).ready(function () {

    for (var i = 0; i < days.length; i++) {
        calculateTotalTime(days[i]);
    }

    $(".toUse").keyup(function () {
        var regex = new RegExp("[0-9]+$");
        var selector = $("#" + this.id.toString());
        var firstWordId = this.id.toString().split('-');
        if (selector.val() == "") {
            selector.css({ 'border-color': 'green' });
            calculateTotalTime(firstWordId[0]);
            return;
        }
        if (!regex.test(selector.val())) {
            selector.css({ 'border-color': 'red' });
            statustime = false;
        } else {
            if (parseInt(selector.val()) > 1440 || parseInt(selector.val()) <= 0 || selector.val().indexOf('-')>=0) {
                selector.css({ 'border-color': 'red' });
            } else {
                selector.css({ 'border-color': 'green' });
            }
            calculateTotalTime(firstWordId[0]);
        }
    });

});

function clickHome() {
    window.location.href = "/MainWindow.aspx";
    return false;
}

function editBtnClick() {
    $(".toUse").removeAttr("readonly").css({ 'border-color': 'green' });
    $("#editBtn").hide();
    $("#saveBtn").show();
    return false;
}

function saveBtnClick() {
    if (statustime) {
        $(".toUse").attr("readonly", "readonly").css({ 'border-color': 'gray' });
        var info = "";
        $(".toUse").each(function () {
            var value = "";
            if ($(this).val()=="") {
                value = "null";
            } else {
                value = $(this).val();
            }
            info += this.id.toString() + '~' + value+'@';
        });
        __doPostBack('changeWeekData', info);
    } else {
        alert("Not all fields filled in correctly!!! Please correct them");
    }
    return false;
}

function calculateTotalTime(day) {
    var result = 0;
    $(".inputBox").each(function () {
        if (this.id.toString().indexOf(day) >= 0) {
            if ($(this).val() != "") {
                result += parseInt($(this).val());
            }
        }
    });
    $('#' + day).text(result+" мин.");
    if (result > 1440 || result < 0) {
        $('#' + day).css({ 'color': 'red' });
        statustime = false;
    } else {
        $('#' + day).css({ 'color': 'black' });
        statustime = true;
    }
}